#include "commn.h"
#include <stdlib.h>
#include <wait.h>


int main()
{
  umask(0);
  if(mkfifo(MY_PATH, 0666) < 0) {
    perror("fifo");
    return 1;
  }

  int fd = open(MY_PATH, O_RDONLY);
  if(fd < 0) {
    perror("open");
    return 2;
  }
  while(1) {
    char buffer[64] = {0};
    ssize_t s = read(fd, buffer, sizeof(buffer)-1);
    if(s > 0) {
      buffer[s-1] = 0;
      if(!strcmp(buffer, "show")) {
        if(fork() == 0) {
          execl("/usr/bin/ls", "ls", "-l", "-h", NULL);
          exit(0);
        }
        waitpid(-1, NULL, 0);
      }
      else if(!strcmp(buffer, "run")) {
        if(fork() == 0) {
          execl("/usr/bin/sl", NULL);
          exit(0);
        }
        waitpid(-1, NULL, 0);
      }else {
        printf("client# %s\n", buffer);
      }

    }else if(s == 0) {
      printf("client quit...\n");
      break;
    }else {
      perror("read");
      break;
    }
  }

  close(fd);
  return 0;
}
